package com.pipe.servicepipe.controller;

import com.pipe.commonutils.R;
import com.pipe.servicepipe.annotation.OperationAnnotation;
import com.pipe.servicepipe.entity.PipeUser;
import com.pipe.servicepipe.service.PipeAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PipeLoginController
 * @Author HETAO
 * @Date 2020/7/19 11:39
 */
@RestController
@RequestMapping("/admin")
@Api("登录界面")
public class PipeAdminController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Resource
    private PipeAdminService pipeAdminService;

    @GetMapping("/login/getCaptcha")
    public R loginBySms(@RequestParam("phone") String phone) {
        pipeAdminService.loginBySms(phone);
        return R.ok();
    }

    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login/checkCaptcha")
    @OperationAnnotation("短信登录")
    public R checkSms(@RequestParam("phone") String phone,
                      @RequestParam("captcha") String captcha) {
        String token = pipeAdminService.loginByPhone(phone, captcha);
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return R.ok().message("登录成功").data(tokenMap);
    }

    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login")
    @OperationAnnotation("账号密码登录")
    public R login(@RequestBody PipeUser pipeUser) throws Exception{
        String token = pipeAdminService.login(pipeUser.getUserName(), pipeUser.getPassword());
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return R.ok().message("登录成功").data(tokenMap);
    }
}
